package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.domain.Travel;
import cn.wolfcode.luowowo.acticle.query.TravelQuery;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.comment.domain.ScenicSpotComment;
import cn.wolfcode.luowowo.comment.service.IScenicSpotCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mine")
public class MineController {

    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private IDestinationService destinationService;
    @Value("${file.path}")
    private String filePath;

    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelRedisService travelRedisService;
    @Reference
    private IStrategyRedisService strategyRedisService;
    @Reference
    private IScenicSpotCommentService scenicSpotCommentService;
    @RequestMapping("/home")
    public String home(@ModelAttribute("qo") TravelQuery qo, Model model, @UserParam UserInfo userInfo) {
        PageInfo pageInfo = travelService.queryByUserId(qo, userInfo.getId());
        List<Travel> pageInfoList = pageInfo.getList();
        for (Travel travel : pageInfoList) {
            int thumbsupnum = travelRedisService.getStatisvo(travel.getId()).getThumbsupnum();
            travel.setRedisThumbsupnum(thumbsupnum);
        }
        model.addAttribute("pageInfo", pageInfo);
        List<UserInfo> list =userInfoService.queryByUserIdForFollow(userInfo.getId());
        //放fans的id设置到home中
        model.addAttribute("idol",list);
        int size = list.size();
        model.addAttribute("travelSize",size);
        return "mine/homepage";
    }
    @RequireLogin
    @RequestMapping("/mytravelnotes")
    public String mytravelnotes(Model model, @UserParam UserInfo userInfo){
        //共享当前用户
        model.addAttribute("userInfo",userInfo);
        //通过当前用户的id 获取到当前用户所写的游记
        Long id = userInfo.getId();
        List<Travel> list = travelService.selectByUserId(id);
        for (Travel travel : list) {
            int thumbsupnum = travelRedisService.getStatisvo(travel.getId()).getThumbsupnum();
            travel.setRedisThumbsupnum(thumbsupnum);
        }
        model.addAttribute("list",list);
        //获取到当前用户游记的总数
        int travelCount = list.size();
        model.addAttribute("travelCount",travelCount);
        //获取到当前用户游记中回复的总数
        //获取当前用户游记的总阅读数
        int viewCount = 0;
        int favorCount = 0;
        for (Travel travel : list) {
            viewCount += travel.getViewnum();
            favorCount += travel.getFavornum();
        }
        model.addAttribute("viewCount",viewCount);
        model.addAttribute("favorCount",favorCount);
        return "mine/mytravelnotes";
    }
    @RequestMapping("review")
    public String review(Model model, @UserParam UserInfo userInfo){
        model.addAttribute("userInfo",userInfo);
        List<ScenicSpotComment> list = scenicSpotCommentService.selectByUserId(userInfo.getId());
        model.addAttribute("list",list);
        model.addAttribute("count",list.size());
        return "mine/review";
    }
    @RequestMapping("travelcollection")
    public String travelcollection(@UserParam UserInfo userInfo, Model model){
        //我收藏的游记
        List<Travel> list =travelRedisService.getTravelByUserId(userInfo.getId());
        model.addAttribute("list",list);
        //我收藏的攻略
        List<StrategyDetail> strategyList = strategyRedisService.getStrategyByUserID(userInfo.getId());
        model.addAttribute("strategyList",strategyList);
        return "mine/travelcollection";
    }
    @RequireLogin
    @RequestMapping("setting")
    public String setting(Model model,@UserParam UserInfo userInfo){
        model.addAttribute("userInfo",userInfoService.get(userInfo.getId()));
        return "mine/setting";
    }
    //跳转到TA的窝主页
    @RequestMapping("personal")
    public String personal(Long fansId,Model model){
        UserInfo userInfo = userInfoService.get(fansId);
        model.addAttribute("userInfo",userInfo);
        List<Travel> travelList = travelService.selectByUserId(fansId);
        model.addAttribute("travelList",travelList);
        model.addAttribute("size",travelList.size());

        return "mine/personal";
    }
    @ResponseBody
    @RequestMapping("/uploadImg")
    public Object uploadImg(MultipartFile files, HttpServletRequest request, HttpServletResponse response, @UserParam UserInfo userInfo) {
        String url = null;
        Map<String, Object> map = new HashMap();
        try {

            url = UploadUtil.upload(files, filePath);
            map.put("status", 1);
            map.put("url", url);
            userInfo.setHeadImgUrl("/" + url);
            userInfoService.updateUserInfo(userInfo);
            String token = CookieUtil.getToken(request, response);
            userInfoRedisService.sendToken(token,userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", 0);
            map.put("msg", "上传失败");

        }
        return map;

    }

    @RequireLogin
    @RequestMapping("/updateUserInfo")
    public String updateUserInfo(HttpServletRequest request, HttpServletResponse response,UserInfo userInfo){

        System.out.println(userInfo);
        UserInfo u = userInfoService.get(userInfo.getId());
        userInfo.setPhone(u.getPhone());
        userInfo.setEmail(u.getEmail());
        userInfo.setLevel(u.getLevel());
        userInfo.setHeadImgUrl(u.getHeadImgUrl());
        userInfo.setState(u.getState());

        //把前端传回来的参数先更新到数据库
        userInfoService.updateUserInfo(userInfo);
        //获取cookie的值
        String token = CookieUtil.getToken(request, response);
        //通过登录拦截重新查询,再把数据缓存
        userInfoRedisService.sendToken(token,userInfo);

        return "mine/setting";
    }

    @RequireLogin
    @RequestMapping("userUpdatePassword")
    public String userUpdatePassword(){


        return "mine/updatepassword";
    }
    @RequireLogin
    @RequestMapping("userUpdatePasswordByEmail")
    public String userUpdatePasswordByEmail(){


        return "mine/updatepasswordbyemail";
    }
    @RequireLogin
    @RequestMapping("userUpdateEmail")
    public String userUpdateEmail(){


        return "mine/updateemail";
    }
    @RequireLogin
    @RequestMapping("userUpdatePhone")
    public String userUpdatePhone(){


        return "mine/updatephone";
    }
    @RequestMapping("/mytravelnotes/travelThumbup")
    @ResponseBody
    public Object travelThumbup(Long sid,@UserParam UserInfo userInfo,Model model) {
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false,"请先进行登录");
            result.setCode(102);
            return result;
        }
        boolean ret = travelRedisService.thumbup(sid, userInfo.getId());
        return new AjaxResult(ret, "").addData(travelRedisService.getStatisvo(sid));
    }
    @RequestMapping("/home/travelThumbup")
    @ResponseBody
    public Object hometravelThumbup(Long sid,@UserParam UserInfo userInfo,Model model) {
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false,"请先进行登录");
            result.setCode(102);
            return result;
        }
        boolean ret = travelRedisService.thumbup(sid, userInfo.getId());
        return new AjaxResult(ret, "").addData(travelRedisService.getStatisvo(sid));
    }
}
